package com.hou.controller;

import com.hou.pojo.Province;
import com.hou.pojo.Result;
import com.hou.service.ProvinceService;
import io.netty.handler.timeout.ReadTimeoutException;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author shkstart
 * @create 2021-09-21-8:54
 */
@RestController
@CrossOrigin
@RequestMapping("province")
public class ProvinceController {

    @Autowired
    private ProvinceService provinceService;

    // 分页查询功能实现
    @GetMapping("findByPage")
    public Map<String, Object> findByPage(@Param("page") Integer page, @Param("rows") Integer rows) {
        //空值处理
        page = page == null ? 1 : page;
        rows = rows == null ? 5 : rows;
        HashMap<String, Object> map = new HashMap<>();
        Integer totalPage;
        //分页数据
        List<Province> provinces = provinceService.findByPage(page, rows);
        //所有数据
        Integer totals = provinceService.findTotals();
        //计算总页数
        totalPage = totals % rows == 0 ? totals / rows : totals / rows + 1;
        //将数据放入map中
        map.put("provinces", provinces);
        map.put("totals", totals);
        map.put("totalPage", totalPage);
        map.put("page", page);
        return map;
    }

    // 显示所有省份
    @GetMapping ("getAllProvince")
    public List<Province> getAllProvince(){
        List<Province> allProvince = provinceService.getAllProvince();
        return allProvince;
    }


    // 添加省份
    @PostMapping("saveProvinceInfo")
    public Result saveProvinceInfo(@RequestBody Province province) {
        Result result = new Result();
        try {
            provinceService.saveProvince(province);
            result.setMsg("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setStatue(false);
            result.setMsg("保存失败");
        }
        return result;
    }

    // 删除省份
    @GetMapping("deleteProvince")
    public Result deleteProvince(Integer id){
        Result result = new Result();
        try {
            provinceService.deleteProvince(id);
            result.setStatue(true);
            result.setMsg("删除成功");
        } catch (Exception e){
            e.printStackTrace();
            result.setStatue(false);
            result.setMsg("删除失败");
        }
        return result;
    }

    // 省份信息回显
    @GetMapping("findProvinceById")
    public Province findProvinceById(Integer id){
        Province province = provinceService.findProvinceById(id);
        return province;
    }

    // 更改省份
    @PostMapping("updateProvince")
    public Result updateProvince(@RequestBody Province province){
        Result result = new Result();
        try {
            provinceService.updateProvince(province);
            result.setStatue(true).setMsg("更新成功");
        } catch (Exception e){
            result.setStatue(false).setMsg("更新失败");
            e.printStackTrace();
        }
        return result;
    }

}
